﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class PlatformTool : MonoBehaviour
{
    public static PlatformTool Instance;
#if UNITY_ANDROID
    AndroidJavaClass jc;
    //AndroidJavaObject jo;
#endif

    void Awake()
    {
        Instance = this;
#if UNITY_EDITOR

#elif UNITY_ANDROID
        jc = new AndroidJavaClass("com.unity3d.player.Tool");//MainActivity
        SendString("listeningSignal",null);
#elif UNITY_IPHONE
#endif
    }
    public void SendString(string cmd, string str)
    {
#if UNITY_EDITOR

#elif UNITY_ANDROID
        //jo.Call("CmdString",cmd, str);
        jc.CallStatic("CmdString",cmd, str);
#endif
    }
    public void SendJson(string cmd, string json)
    {
#if UNITY_EDITOR

#elif UNITY_ANDROID
        //jo.Call("CmdJson", cmd, json);
#endif
    }
    public void ReceiveString(string cmd, string json)
    {

    }
    public void CmdJson(string json)
    {

    }
#if UNITY_ANDROID
    public byte[] ReadFile(string path)
    {
        try
        {
            sbyte[] signed = jc.CallStatic<sbyte[]>("ReadFile", path);
            byte[] unsigned = new byte[signed.Length];
            Buffer.BlockCopy(signed, 0, unsigned, 0, signed.Length);
            return unsigned;
        }catch(Exception ex)
        {
            Debug.Log(ex.StackTrace);
        }
        return null;
    }
    /// <summary>
    /// 获取蜂窝数据类型
    /// </summary>
    /// <returns></returns>
    public int GetCellType()
    {
        try
        {
            return jc.CallStatic<int>("GetCellType");
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
        }
        return 0;
    }
    /// <summary>
    /// 获取蜂窝数据信号强度
    /// </summary>
    /// <returns></returns>
    public int GetSignalStrength()
    {
        try
        {
            return jc.CallStatic<int>("GetSignalStrenth");
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
        }
        return 0;
    }
    /// <summary>
    /// 获取wifi信号强度
    /// </summary>
    /// <returns></returns>
    public int GetWifiStrength()
    {
        try
        {
            string winfo = jc.CallStatic<string>("GetWifiInfo");
            if (winfo == null | winfo == "")
                return 0;
            string[] infos = winfo.Split('|');
            int strength = 0;
            int.TryParse(infos[0],out strength);
            return strength;
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
        }
        return 0;
    }
    public bool IsEmulator()
    {
        try
        {
            return jc.CallStatic<bool>("isEmulator");
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
            return true;
        }
    }

    public bool UnpakFileAsync(string file, string folder)
    {
        try
        {
            return jc.CallStatic<bool>("UnpackFile", file, folder);
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
            return false;
        }
    }
    public string GetUnpackInfo()
    {
        try
        {
            return jc.CallStatic<String>("GetUnpackInfo");
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
            return "";
        }
    }
    public bool CheckAssets(string assetsName)
    {
        try
        {
            return jc.CallStatic<bool>("CheckAssets",assetsName);
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
            return false;
        }
    }
#else
    public bool IsEmulator()
    {
        return false;
    }
     public string GetUnpackInfo()
    {
        return "";
    }
    public bool UnpakFileAsync(string file, string folder)
    {
        return false;
    }
#endif

    public void GetNetworkInfo()
    {
        // 非手持设备，默认连接wifi的
        //switch (Application.internetReachability)
        //{
        //    case NetworkReachability.NotReachable:
        //        string str = "网络已断开，请重连！！！";
        //        break;
        //    case NetworkReachability.ReachableViaCarrierDataNetwork:
        //        str = "当前使用的是移动网络，请问是否更新！！！";
        //        break;
        //    case NetworkReachability.ReachableViaLocalAreaNetwork:
        //        str = "当前使用的是wifi网络，请问是否更新！！！";
        //        break;
        //    default:
        //        break;
        //}
    }
    
    public void OpenAppStore()
    {
        //UnityEngine.iOS.Device.RequestStoreReview();
#if UNITY_IPHONE || UNITY_EDITOR || UNITY_IOS
        const string APP_ID = "1361497551";
        var url = string.Format(
            "itms-apps://itunes.apple.com/cn/app/id{0}?mt=8&action=write-review",
            APP_ID);
        Application.OpenURL(url);
#endif
    }
}
